<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if ($req->name == '') {
      echo '名字不能为空';
      return;
    }
    if ($req->email == '') {
      echo '邮箱不能为空';
      return;
    }
    if ($req->message == '') {
      echo '留言不能为空';
      return;
    }
    if (!filter_var($req->email, FILTER_VALIDATE_EMAIL)) {
      echo '邮箱地址不正确';
      return;
    }
    
    if (!isset($_SESSION['profile'])) {
      $rslt = $orm->get('pb_ecom_admin', NULL, NULL, NULL, NULL, NULL)->d;
      if (count($rslt) > 0) {
        $_SESSION['profile'] = $rslt[0];
      }
    }
    try {
      require_once '../3rd_party/phpMailer/class.phpmailer.php';
      $mailer = new PHPMailer();
      $mailer->IsSMTP();
      $mailer->Host = SMTP_HOST;
      $mailer->CharSet = "UTF-8";
      
      $mailer->SMTPAuth = true;
      $mailer->Username = SMTP_USER_ATDN;
      $mailer->Password = SMTP_PASS_ATDN;
      $mailer->From = $req->email;
      $mailer->FromName = $req->name;
      
      $mailer->Body = 'Name: ' . $req->name . '<br>Email: ' . $req->email . '<br>Contact No: ' . $req->phone . '<br>Message: ' . $req->message;;
      $mailer->Subject = $_SESSION['profile']->shop_name . ' Enquiry';
    
      $mailer->isHTML(true);
      $mailer->AddAddress($_SESSION['profile']->email, '');
      $mailer->Send();
    
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }
  }
}
?>